/*
 * @Author: zjj
 * @Date: 2019-09-02 16:39:15
 * @LastEditors  : zjj
 * @LastEditTime : 2020-01-02 14:48:19
 */
import mpx from '@mpxjs/core'
import mpxFetch from '@mpxjs/fetch'

mpx.use(mpxFetch)
mpx.xfetch.interceptors.request.use(function(config) {
    var token = wx.getStorageSync('token')
    if (token) {
        config.headers={authorization:"bearer " + token}
    }  
    config.emulateJSON=true // 等价于header = {'content-type': 'application/x-www-form-urlencoded'}
    // 也可以返回promise
    return config 
})
mpx.xfetch.interceptors.response.use(function(res) {
    if(res.requestConfig.url.includes("https://restapi.amap.com")){
        return res
    }
    if(res.data.code!=200){
        console.log("res.data.code",res.data.code)
        mpx.showToast({title:res.data.result,icon:"none"})
        if(res.data.code==401){
            mpx.switchTab({ url: '/pages/home/home' }).then(res=>{
                mpx.navigateTo({url:'/pages/login'+"?isBack="+1})
            });
           
           
            return Promise.reject(res.data.result)
        }
        return Promise.reject(res.data.result)
    }
    // 也可以返回promise
    else{
        if(!res.data.msg){
            mpx.showToast({title:res.data.result,icon:"none"})
            if(res.data.result=="手机号码未绑定"){
                mpx.navigateTo({url:`/pages/quickLogin?type=2`})
            }
            return Promise.reject(res.data.result)
        }
        return res
    }
   
})
